package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.PopularWorkersEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@author 李昭
*@date 2020/9/18
*description:
*/
public class PopularWorkersModel extends AndroidViewModel {

    private PickSelectEntity all;

    private int page = 1;
    private int limit = 10;
    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<PopularWorkersEntity>> popularList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> typeList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> secondCategoryList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> gmList = new MutableLiveData<>();
    public MutableLiveData<String> urlStr = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> cityList = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> categoryType = new MutableLiveData<>();

    public PopularWorkersModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getNewList(String provinceNo, String areaNo, String firstCategoryNo, String secondCategoryNo, String thirdCategoryNo, String fourCategoryNo, String fiveCategoryNo, String sixCategoryNo, String teamSize, String cityNo, boolean b) {
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("provinceNo", provinceNo);
        map.put("areaNo", areaNo);
        map.put("limit", limit);
        map.put("page", page);
        map.put("cityNo", cityNo);
        if (!"".equals(firstCategoryNo) && firstCategoryNo != null) {
            map.put("firstCategoryNo", firstCategoryNo);
        }
        if (!"".equals(secondCategoryNo) && secondCategoryNo != null) {
            map.put("secondCategoryNo", secondCategoryNo);
        }
        if (!"".equals(thirdCategoryNo) && thirdCategoryNo != null) {
            map.put("thirdCategoryNo", thirdCategoryNo);
        }
        if (!"".equals(fourCategoryNo) && fourCategoryNo != null) {
            map.put("fourCategoryNo", fourCategoryNo);
        }
        if (!"".equals(fiveCategoryNo) && fiveCategoryNo != null) {
            map.put("fifthCategoryNo", fiveCategoryNo);
        }
        if (!"".equals(sixCategoryNo) && sixCategoryNo != null) {
            map.put("sixthCategoryNo", sixCategoryNo);
        }
        if (!"".equals(teamSize) && teamSize != null) {
            map.put("teamNumber", teamSize);
        }

        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getPopularWorkers(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<PopularWorkersEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<PopularWorkersEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                popularList.postValue(data.getData().getList());
            }
        });
    }

    public void getProTypeList(String areaCode) {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getProTypeList(areaCode).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                PickSelectEntity pickSelectEntity = new PickSelectEntity();
                pickSelectEntity.setNo("");
                pickSelectEntity.setProjectscale("全部");
                data.getData().add(0, pickSelectEntity);
                typeList.postValue(data.getData());
            }
        });
    }

    public void getSecondCategory(String cid) {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getSecondCategory(cid).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> entityList = data.getData();
                entityList.add(0, all);
                secondCategoryList.postValue(entityList);
            }
        });
    }

    public void getGm(String areaCode) {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getTeamScale(areaCode, "0").enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> entityList = data.getData();
                entityList.add(0,all);
                gmList.postValue(entityList);
            }
        });
    }

    public void getH5Url(String id) {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting("constructteamdetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                String url = data.getData().getUrl() + "?id=" + id + "&type=1";
                urlStr.postValue(url);
            }
        });
    }

    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                cityList.postValue(data.getData());
            }
        });
    }

    /**
     * 班组工队（施工队）详情h5链接请求
     * @param no
     */
    public void TeamParticulars(String no) {
        loading.postValue(true);
        APIManager.getInstance().getPickAPI().getUrlSetting("teamWorkDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    //参数1:type(0:	前台,1:用户中心);	参数2:no
                    String url = data.getData().getUrl() + "?no=" + no + "&type=0";
                    urlStr.postValue(url + "----" +no);
                }
            }
        });
    }

    public void getCategoryType() {
        loading.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getCategoryListTree().enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                data.getData().add(0,new PickFilterEntity("","全部",""));
                categoryType.postValue(data.getData());
            }
        });
    }
}
